<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
    >
      <el-form-item prop="eList" label="器材名称" class="role-list">
        <el-select
          v-model="dataForm.eid"
          :disabled="this.dataForm.id"
          placeholder="器材名称"
        >
          <el-option
            v-for="e in eList"
            :key="e.id"
            :label="e.ename"
            :value="e.id"
            @click.native="getOutNumber()"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="uid">
        <el-input
          v-model="dataForm.uid"
          disabled
          placeholder="用户编号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="uList" label="用户名" class="role-list">
        <el-select v-model="dataForm.uid" placeholder="用户名">
          <el-option
            v-for="u in uList"
            :key="u.id"
            :label="u.username"
            :value="u.id"
            @click.native="getOutNumber()"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="归还数量" prop="inNum">
        <el-input
          v-model="dataForm.inNum"
          :disabled="!this.dataForm.id"
          placeholder="归还数量"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      eList: [],
      //用户列表
      uList: [],
      username: "",
      dataForm: {
        id: "",
        eid: "",
        uid: "",
        inNum: "",
        remark: "",
        creator: "",
        createDate: "",
      },
    };
  },
  computed: {
    dataRule() {
      return {
        uid: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        inNum: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        remark: [
          {
            required: false,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        // var a = this.getUserInfo();
        // a.then((value) => {
        //   console.log(value);
        // });
        Promise.all([
          this.getUserInfo(),
          this.getEList(),
          this.getUList(),
        ]).then((res) => {
          // console.log("res", res[0].id);
          if (this.dataForm.id) {
            this.getInfo();
          }
        });
      });
    },
    //获取用户列表
    getUList() {
      return this.$http
        .get("/sys/user/list")
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.uList = res.data;
          console.log("this.uList", this.uList);
        })
        .catch(() => {});
    },
    //获取当前登录用户所出借的器械数量
    getOutNumber() {
      this.$http
        .post(`/se/lendlist/outInfo`, { ...this.dataForm })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            this.dataForm.inNum = 0;
            return this.$message.error(res.msg);
          }
          // this.eList = res.data;
          this.dataForm.inNum = res.data;
          console.log("getOutNumber()", res.data);
        })
        .catch(() => {});
    },
    //获取当前登录用户所出借的器械信息
    getEList() {
      this.$http
        .get(`/se/equipment/list`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.eList = res.data;
          console.log("getEList()", res.data);
        })
        .catch(() => {});
    },
    //获取当前登录用户信息
    getUserInfo() {
      return new Promise((resolve, reject) => {
        this.$http
          .get(`/sys/user/info`)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            resolve(res.data);
            // this.dataForm.uid = res.data.id;
            this.username = res.data.username;
            console.log(res.data);
          })
          .catch(() => {});
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/se/returnlist/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/se/returnlist/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
